﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.Task.Team.Entity;

namespace Wicresoft.Task.TFSWeb.Controllers
{
    public class ActionController : Controller
    {
        public ActionResult CodeReview(int id)
        {
            using (TfsAutoNotifyContext TfsDB = new TfsAutoNotifyContext())
            {
                var x = TfsDB.ChangeListInfoes.Where(p => p.ShelvesetId == id).ToList();
                ViewBag.Changes = x;
                var y = TfsDB.WorkItemInfoes.Where(p => p.ShelvesetId == id).ToList();
                ViewBag.Items = y;
                ViewData["Change"] = x.Count;
                ViewData["Item"] = y.Count;
                ViewData["PCGuid"] = TfsDB.ProjectCollections.First(p => p.Collection.Contains(Properties.Settings.Default.TfsUri)).Value;
                var z = TfsDB.ShelveInfoes.Find(id);
                return View(z);
            }
        }
        [HttpPost]
        public ActionResult CodeReview(int id, FormCollection collection)
        {
            try
            {
                string str = "";
                using (TfsAutoNotifyContext TfsDB = new TfsAutoNotifyContext())
                {
                    var z = TfsDB.ShelveInfoes.Find(id);
                    var shevle = TfsDB.ShelveInfoes.Find(id);
                    if (TryUpdateModel(shevle))
                    {
                        if (bool.Parse(Request.Form["IsApproved"]) == true)
                        {
                            shevle.IsApproved = true;
                            str = "您批准了申请";
                        }
                        else
                        {
                            shevle.IsApproved = false;
                            str = "您未批准申请";
                        }
                        TfsDB.SaveChanges();
                    }
                }
                return RedirectToAction("Success", new { result = str });
            }
            catch (Exception x)
            {
                return View();
            }
        }
        public ActionResult Success(string result)
        {
            ViewBag.result = result;

            return View();
        }
    }
}
